<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>开发插件</title>
</head>
<body>
    <script>
        function iife() {
            (function ($) {
                // TODO
            })(jQuery);
        }

        function firstGlobalFn() {
            (function ($) {
                $.sum = function (array) {
                    // TODO
                }
            })(jQuery);
        }

        function sum() {
            (function ($) {
                $.sum = function (array) {
                    var total = 0;

                    $.each(array, function (index, item) {
                        item = $.trim(item);
                        item = parseFloat(item) || 0;
                        total += item;
                    });
                    return total;
                }
            })(jQuery);
        }

        function total() {
            $(document).ready(function () {
                var $inventory = $('#inventory tbody');
                var quantities = $inventory.find('td:nth-child(2)').map(function (index, item) {
                    return $(item).text();
                }).get();
                var sum = $.sum(quantities);
                $('#sum').find('td:nth-child(2)').text(sum);
            });
        }

        function average() {
            (function ($) {
                $.average = function (array) {
                    if ($.isArray(array)) {
                        return $.sum(array) / array.length;
                    }
                    return 0;
                };
            })(jQuery);
        }

        function multipleGlobalFn() {
            (function ($) {
                $.sum = function (array) {
                    var total = 0;
                    $.each(array, function (index, item) {
                        item = $.trim(item);
                        item = parseFloat(item) || 0;
                        total += item;
                    });
                    return total;
                };

                $.average = function (array) {
                    if ($.isArray(array)) {
                        return $.sum(array) / array.length;
                    }
                    return 0;
                }
            })(jQuery);
        }

        function globalFn() {
            $(document).ready(function () {
                var $inventory = $('#inventory tbody');
                var prices = $inventory.find('td:nth-child(3)')
                    .map(function (index, item) {
                        return $(item).text();
                    })
                    .get();
                var average = $.average(prices);
                $('#average').find('td:nth-child(3)').text(average.toFixed());
            });
        }

        function extend() {
            (function ($) {
                $.extend({
                    sum: function (array) {
                        var total = 0;
                        $.each(array, function (index, item) {
                            item = $.trim(item);
                            item = parseFloat(item) || 0;
                            total += item;
                        });
                        return total;
                    },

                    average: function (array) {
                        if ($.isArray(array)) {
                            return $.sum(array) / array.length;
                        }
                        return 0;
                    }
                });
            })(jQuery);
        }

        function namespace() {
            (function ($) {
                $.mathUtils = {
                    sum: function (array) {
                        var total = 0;
                        $.each(array, function (index, item) {
                            item = $.trim(item);
                            item = parseFloat(item) || 0;
                            total += item;
                        });
                        return total;
                    },

                    average: function (array) {
                        if ($.isArray(array)) {
                            return $.mathUtils.sum(array) / array.length;
                        }
                        return 0;
                    }
                }
            })(jQuery);
        }

        function fn() {
            jQuery.fn.myMethod = function () {
                alert('Nothing happens.');
            };

            $('div').myMethod();
        }

        function swapClass() {
            (function ($) {
                $.fn.swapClass = function (class1, class2) {
                    if (this.hasClass(class1)) {
                        this.removeClass(class1).addClass(class2);
                    } else if (this.hasClass(class2)) {
                        this.removeClass(class2).addClass(class1);
                    }
                }
            })(jQuery);

            $(document).ready(function () {
                $('table').click(function () {
                    $('tr').swapClass('one', 'two');
                });
            });
        }

        function implicitEach() {
            (function ($) {
                $.fn.swapClass = function (class1, class2) {
                    this.each(function () {
                        var $element = $(this);
                        if ($element.hasClass(class1)) {
                            $element.removeClass(class1).addClass(class2);
                        } else if ($element.hasClass(class2)) {
                            $element.removeClass(class2).addClass(class1);
                        }
                    });
                }
            })(jQuery);
        }

        function join() {
            (function ($) {
                $.fn.swapClass = function (class1, class2) {
                    return this.each(function () {
                        var $element = $(this);
                        if ($element.hasClass(class1)) {
                            $element.removeClass(class1).addClass(class2);
                        } else if ($element.hasClass(class2)) {
                            $element.removeClass(class2).addClass(class1);
                        }
                    });
                }
            })(jQuery);
        }

        function shadow() {
            (function ($) {
                $.fn.shadow = function () {
                    return this.each(function () {
                        var $originalElement = $(this);
                        for (var i = 0; i < 5; i++) {
                            $originalElement
                                .clone()
                                .css({
                                    position: 'absolute',
                                    left: $originalElement.offset().left + i,
                                    top: $originalElement.offset().top + i,
                                    margin: 0,
                                    zIndex: -1,
                                    opacity: .1
                                })
                                .appendTo('body');
                        }
                    });
                }
            })(jQuery);

            $(document).ready(function () {
                $('h1').shadow();
            });
        }

        function arguments() {
            (function ($) {
                $.fn.shadow = function (options) {
                    return this.each(function () {
                        var $originalElement = $(this);
                        for (var i = 0; i < options.copies; i++) {
                            $originalElement
                                .clone()
                                .css({
                                    position: 'absolute',
                                    left: $originalElement.offset().left + i,
                                    top: $originalElement.offset().top + i,
                                    margin: 0,
                                    zIndex: -1,
                                    opacity: options.opacity
                                })
                                .appendTo('body');
                        }
                    });
                }
            })(jQuery);

            $(document).ready(function () {
                $('h1').shadow({
                    copies: 3,
                    opacity: .25
                });
            });
        }

        function defaultArguments() {
            (function ($) {
                $.fn.shadow = function (options) {
                    var defaults = {
                        copies: 5,
                        opacity: .1
                    };
                    var opts = $.extend(defaults, options);
                    return this.each(function () {
                        var $originalElement = $(this);
                        for (var i = 0; i < opts.copies; i++) {
                            $originalElement
                                .clone()
                                .css({
                                    position: 'absolute',
                                    left: $originalElement.offset().left + i,
                                    top: $originalElement.offset().top + i,
                                    margin: 0,
                                    zIndex: -1,
                                    opacity: opts.opacity
                                })
                                .appendTo('body');
                        }
                    });
                }
            })(jQuery);

            $(document).ready(function () {
                $('h1').shadow({
                    copies: 3
                });
            });
        }

        function fnCallbackArguments() {
            (function ($) {
                $.fn.shadow = function (options) {
                    var defaults = {
                        copies: 5,
                        opacity: .1,
                        copyOffset: function (index) {
                            return { x: index, y: index };
                        }
                    };
                    var opts = $.extend(defaults, options);
                    return this.each(function () {
                        var $originalElement = $(this);
                        for (var i = 0; i < opts.copies; i++) {
                            var offset = opts.copyOffset(i);
                            $originalElement
                                .clone()
                                .css({
                                    position: 'absolute',
                                    left: $originalElement.offset().left + offset.x,
                                    top: $originalElement.offset().top + offset.y,
                                    margin: 0,
                                    zIndex: -1,
                                    opacity: opts.opacity
                                })
                                .appendTo('body');
                        }
                    });
                }
            })(jQuery);

            $(document).ready(function () {
                $('h1').shadow({
                    copyOffset: function (index) {
                        return { x: -index, y: -2 * index };
                    }
                });
            });
        }

        function customFnDefaultsArguments() {
            (function ($) {
                $.fn.shadow = function (options) {
                    var opts = $.extend({}, $.fn.shadow.defaults, options);
                    return this.each(function () {
                        var $originalElement = $(this);
                        for (var i = 0; i < opts.copies; i++) {
                            var offset = opts.copyOffset(i);
                            $originalElement
                                .clone()
                                .css({
                                    position: 'absolute',
                                    left: $originalElement.offset().left + offset.x,
                                    top: $originalElement.offset().top + offset.y,
                                    margin: 0,
                                    zIndex: -1,
                                    opacity: opts.opacity
                                })
                                .appendTo('body');
                        }
                    });
                };

                $.fn.shadow.defaults = {
                    copies: 5,
                    opacity: .1,
                    copyOffset: function (index) {
                        return { x: index, y: index };
                    }
                };
            })(jQuery);

            $(document).ready(function () {
                $.fn.shadow.defaults.copies = 10;
                $('h1').shadow({
                    copyOffset: function (index) {
                        return { x: -index, y: index };
                    }
                });
            });
        }

        function widgetFactory() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        this._tooltip = $('<div></div>')
                            .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                            .hide()
                            .appendTo('body');

                        this.element
                            .addClass('learning-tooltip-trigger')
                            .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                            .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                    }
                });
            })(jQuery);
        }

        function widget() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        this._tooltip = $('<div></div>')
                            .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                            .hide()
                            .appendTo('body');

                        this.element
                            .addClass('learning-tooltip-trigger')
                            .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                            .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                    },

                    _open: function () {
                        var elementOffset = this.element.offset();
                        this._tooltip.css({
                            position: 'absolute',
                            left: elementOffset.left,
                            top: elementOffset.top + this.element.height()
                        }).text(this.element.data('tooltip-text'));
                        this._tooltip.show();
                    },

                    _close: function () {
                        this._tooltip.hide();
                    }
                });
            })(jQuery);

            $(document).ready(function () {
                $('a').tooltip();
            });
        }

        function widgetDestroy() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        this._tooltip = $('<div></div>')
                            .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                            .hide()
                            .appendTo('body');

                        this.element
                            .addClass('learning-tooltip-trigger')
                            .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                            .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                    },

                    _open: function () {
                        var elementOffset = this.element.offset();
                        this._tooltip.css({
                            position: 'absolute',
                            left: elementOffset.left,
                            top: elementOffset.top + this.element.height()
                        }).text(this.element.data('tooltip-text'));
                        this._tooltip.show();
                    },

                    _close: function () {
                        this._tooltip.hide();
                    },

                    destory: function () {
                        this._tooltip.remove();
                        this.element
                            .removeClass('learning-tooltip-trigger')
                            .off('.learning.tooltip');
                        $.Widget.prototype.destory.apply(this, arguments);
                    }
                });
            })(jQuery);

            $(document).ready(function () {
                $('a').tooltip('destroy');
            });
        }

        function widgetEnableDisable() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        if (!this.options.disabled) {
                            this._tooltip = $('<div></div>')
                                .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                                .hide()
                                .appendTo('body');

                            this.element
                                .addClass('learning-tooltip-trigger')
                                .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                                .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                        }
                    },

                    _open: function () {
                        var elementOffset = this.element.offset();
                        this._tooltip.css({
                            position: 'absolute',
                            left: elementOffset.left,
                            top: elementOffset.top + this.element.height()
                        }).text(this.element.data('tooltip-text'));
                        this._tooltip.show();
                    },

                    _close: function () {
                        this._tooltip.hide();
                    },

                    destory: function () {
                        this._tooltip.remove();
                        this.element
                            .removeClass('learning-tooltip-trigger')
                            .off('.learning.tooltip');
                        $.Widget.prototype.destory.apply(this, arguments);
                    }
                });
            })(jQuery);

            $(document).ready(function () {
                $('a').tooltip('disable');
                $('a').tooltip('enable');
            });
        }

        function widgetOptions() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        if (!this.options.disabled) {
                            this._tooltip = $('<div></div>')
                                .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                                .hide()
                                .appendTo('body');

                            this.element
                                .addClass('learning-tooltip-trigger')
                                .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                                .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                        }
                    },

                    _open: function () {
                        var elementOffset = this.element.offset();
                        this._tooltip.css({
                            position: 'absolute',
                            left: elementOffset.left + this.options.offsetX,
                            top: elementOffset.top + this.element.height() + this.options.offsetY
                        }).text(this.options.content.call(this.element[0]));
                        this._tooltip.show();
                    },

                    _close: function () {
                        this._tooltip.hide();
                    },

                    destory: function () {
                        this._tooltip.remove();
                        this.element
                            .removeClass('learning-tooltip-trigger')
                            .off('.learning.tooltip');
                        $.Widget.prototype.destory.apply(this, arguments);
                    },

                    options: {
                        offsetX: 10,
                        offsetY: 10,
                        content: function () {
                            return $(this).data('tooltip-text');
                        }
                    }
                });
            })(jQuery);

            $(document).ready(function () {
                $('a').tooltip({ offsetX: -10, offsetY: 25 });
                $('a').tooltip('option', 'offsetX', 20);
            });
        }

        function widgetActions() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        if (!this.options.disabled) {
                            this._tooltip = $('<div></div>')
                                .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                                .hide()
                                .appendTo('body');

                            this.element
                                .addClass('learning-tooltip-trigger')
                                .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                                .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                        }
                    },

                    _open: function () {
                        var elementOffset = this.element.offset();
                        this._tooltip.css({
                            position: 'absolute',
                            left: elementOffset.left + this.options.offsetX,
                            top: elementOffset.top + this.element.height() + this.options.offsetY
                        }).text(this.options.content.call(this.element[0]));
                        this._tooltip.show();
                    },

                    _close: function () {
                        this._tooltip.hide();
                    },

                    destory: function () {
                        this._tooltip.remove();
                        this.element
                            .removeClass('learning-tooltip-trigger')
                            .off('.learning.tooltip');
                        $.Widget.prototype.destory.apply(this, arguments);
                    },

                    options: {
                        offsetX: 10,
                        offsetY: 10,
                        content: function () {
                            return $(this).data('tooltip-text');
                        }
                    },

                    open: function () {
                        this._open();
                    },

                    close: function () {
                        this._close();
                    }
                });
            })(jQuery);

            $(document).ready(function () {
                $('a').tooltip({ offsetX: -10, offsetY: 25 });
                $('a').tooltip('option', 'offsetX', 20);
                $('a').tooltip('close');
            });
        }

        function widgetEvents() {
            (function ($) {
                $.widget('learning.tooltip', {
                    _create: function () {
                        if (!this.options.disabled) {
                            this._tooltip = $('<div></div>')
                                .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                                .hide()
                                .appendTo('body');

                            this.element
                                .addClass('learning-tooltip-trigger')
                                .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                                .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
                        }
                    },

                    _open: function () {
                        var elementOffset = this.element.offset();
                        this._tooltip.css({
                            position: 'absolute',
                            left: elementOffset.left + this.options.offsetX,
                            top: elementOffset.top + this.element.height() + this.options.offsetY
                        }).text(this.options.content.call(this.element[0]));
                        this._tooltip.show();
                        this._trigger('open');
                    },

                    _close: function () {
                        this._tooltip.hide();
                        this._trigger('close');
                    },

                    destory: function () {
                        this._tooltip.remove();
                        this.element
                            .removeClass('learning-tooltip-trigger')
                            .off('.learning.tooltip');
                        $.Widget.prototype.destory.apply(this, arguments);
                    },

                    options: {
                        offsetX: 10,
                        offsetY: 10,
                        content: function () {
                            return $(this).data('tooltip-text');
                        }
                    },

                    open: function () {
                        this._open();
                    },

                    close: function () {
                        this._close();
                    }
                });
            })(jQuery);

            $(document).ready(function () {
                $(this).on('tooltipopen', function () {
                    console.log('tooltip opened...');
                }).on('tooltipclose', function () {
                    console.log('tooltip closed...');
                });
            });
        }
    </script>
</body>
</html>